package com.yanfan.alarm;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.alarm.domain.NjAlarmDeviceInfo;
import com.yanfan.alarm.domain.NjAlarmRecord;
import com.yanfan.alarm.domain.NjDefenceAreaInfo;
import com.yanfan.alarm.utils.OauthUtil;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.ibms.common.utils.HttpUtils2;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

public class Main {

    @Test
    public void getAlarmStatus() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("deviceCode","1000264");
        //param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                        "https://192.168.254.6:443" +
                                "/evo-apigw/evo-alarm/1.0.0/alarmhosts/one/1000264"
                        ,headers,null);
        JSONArray parseArray = JSON.parseArray(JSONObject.parseObject(jsonObject.get("data").toString())
                .get("value").toString());
        parseArray.stream().forEach(alarmMaster ->{
            List<NjDefenceAreaInfo> defenceAreaList = JSON.parseArray(JSONObject.parseObject(alarmMaster.toString())
                    .get("defenceAreaList").toString(), NjDefenceAreaInfo.class);
        });
        System.out.println(jsonObject);
    }

    @Test
    public void getAlarmRecord() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        //param.put("alarmType",81);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 获取十分钟前的时间
        LocalDateTime tenMinutesAgo = now.minusMinutes(10);
        // 格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime = now.format(formatter);
        String tenMinutesAgoTime = tenMinutesAgo.format(formatter);
        param.put("alarmStartDateString",nowTime);
        param.put("alarmEndDateString",tenMinutesAgoTime);
        JSONObject jsonObject = HttpUtils2.
                sendPostSSLBody(
                        "https://192.168.254.6:443" +
                                "/evo-apigw/evo-event/1.2.0/alarm-record/page"
                        ,headers,param);
        List<NjAlarmRecord> parseArray = JSON.parseArray(JSONObject.parseObject(jsonObject.get("data").toString())
                .get("pageData").toString(), NjAlarmRecord.class);
        System.out.println(parseArray);
    }
}
